package com.xxl.job.admin.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ibm.risk.common.dao.JpaDAO;
import com.ibm.risk.common.vo.PagedQueryParam;
import com.ibm.risk.common.vo.PagedQueryResult;
import com.xxl.job.admin.core.model.JobInfo;
import com.xxl.job.admin.dao.IJobInfoDao;

/**
 * job info
 * @author xuxueli 2016-1-12 18:03:45
 */
@Repository
@Transactional(readOnly = false)
public class JobInfoDaoImpl implements IJobInfoDao {
	
	@Inject
	JpaDAO dao;

	@Override
	@Transactional(readOnly = true)
	public PagedQueryResult<JobInfo> pageList(int offset, int pagesize, Long jobGroup, String executorHandler) {
		CriteriaBuilder cb = dao.getCriteriaBuilder();
		CriteriaQuery<JobInfo> ctQuery = cb.createQuery(JobInfo.class);
		Root<JobInfo> root = ctQuery.from(JobInfo.class);
		// userRoot.alias("u");
		List<Predicate> criteList = new ArrayList<Predicate>();
		if (StringUtils.isNotBlank(executorHandler)) {
			criteList.add(cb.like(cb.upper(root.<String> get("executorHandler")), "%" + executorHandler.trim().toUpperCase() + "%"));
		}
		if (jobGroup > 0) {
			criteList.add(cb.equal(root.<Long> get("jobGroup"), jobGroup));
		}
		Predicate[] predicates = criteList.toArray(new Predicate[criteList.size()]);

		ctQuery.where(predicates);

		PagedQueryParam queryParam = new PagedQueryParam() {
		};
		queryParam.setPageSize(pagesize);
		queryParam.setPageNumber(offset);
		PagedQueryResult<JobInfo> result = dao.executePagedQuery(JobInfo.class, queryParam, ctQuery, predicates);
		return result;
	}


	@Override
	public int save(JobInfo info) {
		info.setAddTime(new Date());
		info.setGlueUpdatetime(info.getAddTime());
		info.setUpdateTime(info.getAddTime());
		dao.persist(info);
		return 1;
	}

	@Override
	@Transactional(readOnly = true)
	public JobInfo loadById(Long id) {
		return dao.find(JobInfo.class, id);
	}

	@Override
	public int update(JobInfo item) {
		item.setUpdateTime(new Date());
		item.setGlueUpdatetime(item.getUpdateTime());
		item = dao.merge(item);
		return 1;
	}

	@Override
	public int delete(Long id) {
		dao.remove(dao.getReference(JobInfo.class, id));
		return 1;
	}

	@Override
	@Transactional(readOnly = true)
	public List<JobInfo> getJobsByGroup(Long jobGroup) {
		return dao.createNamedQuery(JobInfo.QUERY_BY_GROUP, JobInfo.class).setParameter("jobGroup", jobGroup).getResultList();
	}

	@Override
	@Transactional(readOnly = true)
	public int findAllCount() {
		return ((Long) dao.createNamedQuery(JobInfo.COUNT_ALL).getSingleResult()).intValue();
	}

}
